package com.example.t1.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 
 * Copyright (c) 2012 All rights reserved 名称：BeyondEdit.java 描述：默认限制140字
 * 
 * @author 智迅 zhixun123@gmail.com
 * @date：2013-9-4 下午4:24:08
 * @version v1.0
 */
public class BeyondEdit extends EditText {

    /**
     * 字数默认限制
     */
    private int num = 140;;

    private Context context1 = null;

    private String str = "";

    /**
     * 对字符数限制 默认 对数字大小的限制
     */
    private int style = 1;

    public Context getContext1() {
	return context1;
    }

    public void setContext1(Context context) {
	this.context1 = context;
    }

    public BeyondEdit(Context context, AttributeSet attrs, int defStyle) {
	super(context, attrs, defStyle);
	init(context);

    }

    public BeyondEdit(Context context, AttributeSet attrs) {
	super(context, attrs);
	init(context);

    }

    public BeyondEdit(Context context) {
	super(context);
	init(context);

    }

    void init(Context context) {
	setContext1(context);
	addTextChangedListener(textWatcher);
	setLongClickable(false);
    }

    /**
     * 
     * 描述：设置提示语
     * 
     * @throws
     * @date：2013-10-21 下午3:16:00
     * @author 智迅 zhixun123@gmail.com
     * @version v1.0
     */
    public void setAlertMsg(String msg) {
	str = msg;
    }

    /**
     * 
     * 描述： 对字符数数量的限制1 默认 对数字大小的限制2(只支持整形)
     * 
     * @throws
     * @date：2013-9-4 下午4:21:31
     * @author 智迅 zhixun123@gmail.com
     * @version v1.0
     */
    public void setLimitNum(int n, int style) {
	num = n;
	this.style = style;
    }

    public void setLimitNum(int n) {
	num = n;
	this.style = 1;
    }

    TextWatcher textWatcher = new TextWatcher() {

	private CharSequence temp;
	private int selectionStart;
	private int selectionEnd;

	public void beforeTextChanged(CharSequence s, int start, int count,
		int after) {
	}

	public void onTextChanged(CharSequence s, int start, int before,
		int count) {
	    temp = s;
	}

	public void afterTextChanged(Editable s) {
	    // int number = num - s.length();
	    // hasnumTV.setText("" + number);
	    selectionStart = getSelectionStart();
	    selectionEnd = getSelectionEnd();
	    if (style == 1) {
		if (temp.length() > num) {
		    removeTextChangedListener(this);
		    s.delete(num, selectionEnd);
		    setText(s.toString());
		    setSelection(num);// 设置光标在最
		    Toast.makeText(getContext1(), str, Toast.LENGTH_LONG)
			    .show();
		    addTextChangedListener(this);
		}
	    } else {
		removeTextChangedListener(this);
		if ("".equals(s.toString())) {
		    setText("1");
		    addTextChangedListener(this);
		    return;
		}
		if (Integer.valueOf(s.toString()) > num) {
		    setText(num + "");
		    Toast.makeText(getContext1(), str, Toast.LENGTH_LONG)
			    .show();
		}
		addTextChangedListener(this);
	    }

	}
    };
}
